<?php

/**
 * notes actions.
 *
 * @package    retailbrain
 * @subpackage notes
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class notesActions extends sfActions {

    /**
     * Termina la session por inactividad
     */
    public function preExecute() {
        if (!$this->getUser()->isAuthenticated()) {
            $this->getUser()->setAuthenticated(false);
            $this->getUser()->getAttributeHolder()->clear();
            $this->redirect('/login');
        }
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->title = "Notas";
        $this->breadcrumbs = array(array("Notas", "notes/index"));

        $user = $this->getUser()->getAttribute('id_users');
        $this->caps = $this->getUser()->getAttribute('caps');
        
        
         $q = Doctrine_Query::create()
                ->from('UserTable ut')
                ->leftJoin('ut.UserTableOrder uto')
                ->where('ut.table_user = ?', array('Notas'))
                ->addWhere('uto.id_users = ?', array($this->getUser()->getAttribute('id_users')))
                ->orderBy('uto.orden');

        $this->userTableOrder = $q->fetchArray();

        $this->userOrderCount = count($q->fetchArray());


        if (!$this->userOrderCount) {
            $q = Doctrine_Query::create()
                    ->from('UserTable ut')
                    ->leftJoin('ut.UserTableOrder uto')
                    ->where('ut.table_user = ?', array('Notas'))
                    ->orderBy('ut.id_tablecolumn');

            $this->userTableOrder = $q->fetchArray();
        }
        
        $id = $request->getParameter('id');
        
         /** Notes * */
        $q = Doctrine_Query::create()
                ->from('StoreNotes sn')
                ->innerJoin('sn.Stores s')
                ->innerJoin('s.Formats f')
                ->innerJoin('sn.Users u')
                ->innerJoin('sn.CategoryNote cn')
                ->where('sn.id_store = ?', array($id))
                ->orderBy('sn.note_date');

        $this->notes = $q->fetchArray();
        
        
    }

    
    
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateOrder(sfWebRequest $request) {


        $order = $request->getParameter('order');
        $visible = $request->getParameter('visible');
        $table_column = $request->getParameter('tableColumn');

        $user = $this->getUser()->getAttribute('id_users');

        myFunctionsTables::setOrderTable($table_column, $order, $visible, $user, "Notas");

        $this->redirect('/notes/index');
    }
    
    
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeClearOrder(sfWebRequest $request) {

        $this->setLayout(false);
        $user = $this->getUser()->getAttribute('id_users');

        $this->forward404Unless($order = Doctrine::getTable('UserTableOrder')->findBySql("id_users = ? and table_user = ?", array($user,"Notas")));
        try {
            $order->delete();
        } catch (Exception $e) {
            //$this->redirect('cliente/index');
        }

        $this->redirect('/notes/index');
    }
    
    
    
    public function executeExcelNotes(sfWebRequest $request) {

        $objPHPExcel = new PHPExcel();

        // Set properties
        $objPHPExcel->getProperties()->setCreator("RetailBrain");
        $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
        $objPHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);

        // Add some data
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0,1, 'FECHA');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1,1,  'CADENA');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2,1,  'LOCAL');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3,1,  'OBSERVACIÓN');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4,1,  'AUTOR');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5,1,  'CATEGORÍA');
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6,1,  'ESTADO');
        
        
        
        $user = $this->getUser()->getAttribute('id_users');

        $objPHPExcel->getActiveSheet()->setTitle('Notas');

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="notas.xlsx"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;

      return sfview::NONE;
    }

    
    
    /**
     * NoteActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActiveNoteInformations(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $id_store = $request->getParameter('id_store');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('StoreNotes n')
                ->set('n.active', '?', $active)
                ->where('n.id_note = ?', array($id))
                ->execute();

       /** Notes * */
        $q = Doctrine_Query::create()
                ->from('StoreNotes sn')
                ->innerJoin('sn.Users s')
                ->where('sn.id_store = ?', array($id_store))
                ->orderBy('sn.note_date desc');

        $this->notes = $q->fetchArray();

        /** Categorys * */
        $q = Doctrine_Query::create()
                ->from('CategoryNote cn')
                ->orderBy('cn.name');

        $this->categorys = $q->fetchArray();



        $this->setTemplate('lstNotesInformations');
    }
}
